package com.herench.bs.transfer;

import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2017/11/2.
 */
public class LoadLabMasterDetailsTransfer extends AbstractTransfer{
    public LoadLabMasterDetailsTransfer(String method,Client client,String messageMethod) {super(method, client,messageMethod);}

    @Override
    public String toInvokeMessage(Map<String,Object>params) throws Exception{
        String repId = (String) params.get("repId");
        String cardNo = (String) params.get("cardNo");
        Document tplDoc = getTplDoc(getMethod());
        Element dataRowElement = tplDoc.getRootElement().element("CommitData").addElement("DataNode1").addElement("DataRow");
        dataRowElement.addAttribute("TEST_NO", repId).addAttribute("ID_NO ", cardNo);
        return tplDoc.asXML();
    }
    @Override
    public Map<String, Object> toResultMessage(String resMsg)throws Exception{
        Map<String, Object>resMap = new HashMap<>();
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (executeStatus.equals("0")){
            List<Element> dataRows = document.getRootElement().element("ReturnData").element("Data").elements();
            List<Map<String, Object>>dataList = new ArrayList<>();
            if(dataRows !=null && dataRows.size() > 0) {
                for (Element dataRow : dataRows) {
                    Map<String, Object> dataMap = new HashMap<>();
                    LoadLabMasterDetailsTransfer.LoadLabMasterDetailsInnerMessage dataRowMessage = new LoadLabMasterDetailsTransfer.LoadLabMasterDetailsInnerMessage(dataRow);
                    dataMap.put("repId",dataRowMessage.getRepId());
                    dataMap.put("itemCode",dataRowMessage.getItemCode());
                    dataMap.put("repName",dataRowMessage.getRepName());
                    dataMap.put("itemName",dataRowMessage.getItemName());
                    dataMap.put("itemVal",dataRowMessage.getItemVal());
                    dataMap.put("itemUnits",dataRowMessage.getItemUnits());
                    dataMap.put("normalVal",dataRowMessage.getNormalVal());
                    dataMap.put("flag",dataRowMessage.getFlag());
                    dataList.add(dataMap);
                }
            }
            resMap.put("list",dataList);
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
            return resMap;
        } else {
            resMap.put("code", executeStatus);
            resMap.put("messageIn", executeMessage);

        }
        return resMap;
    }
    private class LoadLabMasterDetailsInnerMessage implements InnerMessage{
        /**
         * 报告单号
         */
        private String repId;
        /**
         * 项目编码
         */
        private String itemCode;
        /**
         * 报告名称
         */
        private String repName;
        /**
         * 项目名称
         */
        private String itemName;
        /**
         * 检验结果
         */
        private String itemVal;
        /**
         * 检查结果单位
         */
        private String itemUnits;
        /**
         * 检查结果参考范围
         */
        private String normalVal;
        /**
         * 检查结果异常标识
         */
        private String flag;

        public LoadLabMasterDetailsInnerMessage(Element element){
            this.repId = element.attributeValue("TEST_NO");
            this.itemCode = element.attributeValue("ITEM_CODE");
            this.repName = element.attributeValue("REPORT_ITEM_NAME");
            this.itemName = element.attributeValue("ITEM_NAME");
            this.itemVal = element.attributeValue("RESULT");
            this.itemUnits = element.attributeValue("UNITS");
            this.normalVal = element.attributeValue("REF_RANGE");
            this.flag = element.attributeValue("ABNORMAL_INDICATOR");



        }
        public String getRepId() { return repId; }

        public String getItemCode() { return itemCode; }

        public String getRepName() { return repName; }

        public String getItemName() { return itemName; }

        public String getItemVal() { return itemVal; }

        public String getItemUnits() { return itemUnits; }

        public String getNormalVal() { return normalVal; }

        public String getFlag() { return flag; }
    }


}
